package pro.seac.twitter.domain;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;

@Entity
public class Message extends BaseEntity {

	@Column(length = 150, nullable = false)
	private String content;

	@ManyToOne(fetch = FetchType.LAZY, optional = false)
	@JoinColumn(name = "senderId")
	private UserAccount sender;

	@ManyToOne(fetch = FetchType.LAZY, optional = true)
	@JoinColumn(name = "addresseeId")
	private UserAccount addressee;

	@ManyToMany(fetch = FetchType.LAZY)
	@JoinTable(name = "mention", joinColumns = { @JoinColumn(name = "messageId", referencedColumnName = "id") }, inverseJoinColumns = { @JoinColumn(name = "userId", referencedColumnName = "id") })
	private Set<UserAccount> mentioned;

	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public UserAccount getSender() {
		return sender;
	}

	public void setSender(UserAccount sender) {
		this.sender = sender;
	}

	public UserAccount getAddressee() {
		return addressee;
	}

	public void setAddressee(UserAccount addressee) {
		this.addressee = addressee;
	}

	public Set<UserAccount> getMentioned() {
		return mentioned;
	}

	public void setMentioned(Set<UserAccount> mentioned) {
		this.mentioned = mentioned;
	}

	public void addMentioned(UserAccount user) {
		if (mentioned == null) mentioned = new HashSet<UserAccount>();
		mentioned.add(user);
	}
}
